/*
 * QRadioButtonAdapter.cpp  0.4.0 / Nov 29, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "QRadioButtonAdapter.h"

// QRadioButtonAdapter :: Public Constructor. ----------------------------------

/*!
 * \brief Constructs new QRadioButtonAdapter object.
 */
QRadioButtonAdapter::QRadioButtonAdapter()
  : QRadioButton()
{
  setAutoExclusive(false);
}

// QRadioButtonAdapter :: Overridden Member Functions. -------------------------

/*!
 * \inheritDoc
 */
QSize QRadioButtonAdapter::sizeHint()const
{
  if (icon().isNull()) {
    return QRadioButton::sizeHint();
  }
  ensurePolished();
  QSize sh = iconSize();
  if (!text().isEmpty()) {
    QStyleOptionButton option;
    initStyleOption(&option);
    QRect textRect = style()->itemTextRect(fontMetrics(), QRect(0, 0, 1, 1),
                                           Qt::TextShowMnemonic, isEnabled(),
                                           text());
    sh.setWidth(sh.width() + 5 + textRect.width());
    if (sh.height() < textRect.height()) {
      sh.setHeight(textRect.height());
    }
  }
  sh.setWidth(sh.width() + 2);
  sh.setHeight(sh.height() + 2);
  return sh.expandedTo(QApplication::globalStrut());
}

/*!
 * \inheritDoc
 */
void QRadioButtonAdapter::paintEvent(QPaintEvent *e)
{
  Q_UNUSED(e); // TODO: use event rect
  QStylePainter painter(this);
  QStyleOptionButton option;
  initStyleOption(&option);
  if (icon().isNull()) {
    painter.drawControl(QStyle::CE_RadioButton, option);
  } else {
    painter.drawControl(QStyle::CE_RadioButtonLabel, option);
    if (hasFocus()) {
      QStyleOptionFocusRect focusOption;
      focusOption.initFrom(this);
      focusOption.backgroundColor = palette().color(QPalette::Background);
      if (!text().isEmpty()) {
        focusOption.rect = style()->itemTextRect(fontMetrics(),
                                                 focusOption.rect,
                                                 Qt::TextShowMnemonic,
                                                 isEnabled(), text());
        int dx = option.iconSize.width() + 2;
        int dy = (height() - focusOption.rect.height()) >> 1;
        focusOption.rect.adjust(dx, dy, dx + 4, dy);
      } else {
        focusOption.rect = QRect(0, 1, option.iconSize.width(),
                                 option.iconSize.height());
      }
      style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOption, &painter,
                             this);
    }
  }
}
